rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_NL_14.RData")
#SI A.6: Results belonging to Figure 1-----------------


#Wave 1
vote.pvv.14<-list()
vote.pvv.14[[1]]<- glm(w1_pvv_national ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + female + age, data=data_sub, family="binomial")
vote.pvv.14[[2]]<- glm(w1_pvv_national ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + female + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_cyn) + zero1(w1_immi), data=data_sub, family="binomial")
#Wave 4
vote.pvv.14[[3]]<- glm(w4_pvv_EU_vote ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + female + age, data=data_sub, family="binomial")
vote.pvv.14[[4]]<- glm(w4_pvv_EU_vote ~ zero1(w5_agre) + zero1(w5_open) + zero1(w5_con) + zero1(w5_extra) + zero1(w5_neu) + female + age + prep_secon + highschool + secondary_vocation + pre_uni + college + uni + income + income_missing + zero1(w1_cyn) + zero1(w1_immi), data=data_sub, family="binomial")
labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age", "Prepatory secondary education", "High school (first 3 years)", "Secondary vocational", "Some college", "College", "University", "Income", "Income missing", "Political cynicism", "Social conservatism")
stargazer2(vote.pvv.14, odd.ratio=TRUE, title="Dutch EU Election Study 2014; Vote for PVV", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), covariate.labels = labels,dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base - 13", "Figure 1 - 13", "Base - 14", "Figure 1 - 14"), notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/Dutch_results.tex", no.space=TRUE, label="tab:DutchEU_results")

#SI A.6: Descriptive statistics---------
desc.labels <- c("Populist Vote",labels)
stargazer(vote.pvv.14[[2]]$model, type = "latex", summary.stat = c("mean", "sd", "N", "median","min",  "max"), covariate.labels=desc.labels, title="Descriptive statistics Dutch EU Election Study 2014", out="Tables/NL_descripEU.tex", no.space=TRUE, label="tab:NL_descriptivesEU", digits=2)

#Alpha Openness
open_a<-data.frame(data_sub$w5_open1, data_sub$w5_open2_rec, data_sub$w5_open3_rec, data_sub$w5_open4_rec)
psych::alpha(open_a, keys=TRUE)
#Alpha Conscientiousness
open_c<-data.frame(data_sub$w5_con1, data_sub$w5_con2, data_sub$w5_con3_rec, data_sub$w5_con4_rec)
psych::alpha(open_c, keys=TRUE)
#Alpha Extraversion
ext_a<-data.frame(data_sub$w5_ext1, data_sub$w5_ext2, data_sub$w5_ext3_rec, data_sub$w5_ext4_rec)
psych::alpha(ext_a, keys=TRUE)
#Alpha Agreeableness
agre_a<-data.frame(data_sub$w5_agre1, data_sub$w5_agre2, data_sub$w5_agre3_rec, data_sub$w5_agre4_rec)
psych::alpha(agre_a, keys=TRUE)
#Alphs Openness
neu_a<-data.frame(data_sub$w5_neu1, data_sub$w5_neu2, data_sub$w5_neu3_rec, data_sub$w5_neu4_rec)
psych::alpha(neu_a, keys=TRUE)
#Alpha social conservatism
alpha_immi<-data.frame(data_sub$immi1, data_sub$immi2, data_sub$immi3, data_sub$immi4,data_sub$immi5)
psych::alpha(alpha_immi)
#Alpha cynicism
alpha_cyn<-data.frame(data_sub$cyn1, data_sub$cyn2, data_sub$cyn3, data_sub$cyn4)
psych::alpha(alpha_cyn)

#Median income
median(as.numeric(data_sub$w1_q65)[as.numeric(data_sub$w1_q65)<12])
table(data_sub$w1_q65)

#SI A.6: Factor loadings ------------------------
latent<-(with(data_sub, data.frame(w5_open1, w5_open2_rec, w5_open3_rec, w5_open4_rec, w5_con1, w5_con2, w5_con3_rec, w5_con4_rec, w5_ext1, w5_ext2, w5_ext3_rec, w5_ext4_rec, w5_agre1, w5_agre2, w5_agre3_rec, w5_agre4_rec, w5_neu1, w5_neu2, w5_neu3_rec, w5_neu4_rec)))
latent <- na.omit(latent)
CFA_agree <-'Agree = ~ NA*w5_agre1+ w5_agre2+ w5_agre3_rec+ w5_agre4_rec 
Open = ~ NA*w5_open1+ w5_open2_rec+ w5_open3_rec+ w5_open4_rec
Con = ~ NA*w5_con1+ w5_con2+ w5_con3_rec+ w5_con4_rec
Ext = ~ NA*w5_ext1+ w5_ext2+ w5_ext3_rec+ w5_ext4_rec 
Neu = ~ NA*w5_neu1+ w5_neu2+ w5_neu3_rec+ w5_neu4_rec
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 
fit<-cfa(CFA_agree,  data=latent)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:20, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Agreeableness item 4", "Openness item 1", "Openness item 2", "Openness item 3", "Openness item 4", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3", "Conscientiousness item 4",  "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Extraversion item 4", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3", "Neuroticism item 4")))

p<-xtable(caption = "Dutch EU Election Study 2014: Big Five Standardized Factor Loadings", label = "tab:NL14_cfa", p)
print(p, type="latex", file="Tables/NL_factor14.tex", size="tiny", caption.placement="top")

#SI A.6: Correlation--------------------------
myvars <- c("w5_agre", "w5_open", "w5_con", "w5_extra", "w5_neu", "w1_cyn" , "w1_immi")
cor_ivs <- data_sub[myvars]

names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Social conservatism", "7. Economic conservatism")

correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Dutch EU Election Study 2014: Correlation Matrix of Independent Variables", out="Tables/NL_corEU.tex", no.space=TRUE, label="tab:NL_corEU", digits=2)




